<template>
  <div class="system-config">
    <div class="page-header">
      <div class="header-left">
        <h2>系统配置</h2>
      </div>
      <div class="header-right">
        <el-button type="danger" @click="handleBatchDelete" :disabled="selectedConfigs.length === 0">
          <el-icon><Delete /></el-icon>批量删除
        </el-button>
        <el-button type="primary" @click="showCreateDialog">
          <el-icon><Plus /></el-icon>新建配置
        </el-button>
        <el-button type="success" @click="refreshCache">
          <el-icon><RefreshRight /></el-icon>刷新缓存
        </el-button>
      </div>
    </div>

    <!-- 查询条件 -->
    <div class="search-panel">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="配置名称">
          <el-input v-model="searchForm.key" placeholder="请输入配置名称" clearable />
        </el-form-item>
        <el-form-item label="配置项">
          <el-input v-model="searchForm.value" placeholder="请输入配置值" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>查询
          </el-button>
          <el-button @click="resetSearch">
            <el-icon><RefreshRight /></el-icon>重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    
    <!-- 配置列表 -->
    <div class="table-container">
      <el-table
        v-loading="loading.configs"
        :data="configs"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column prop="key" label="配置名称" min-width="180" />
        <el-table-column prop="value" label="配置值" min-width="200" show-overflow-tooltip />
        <el-table-column prop="description" label="描述" min-width="200" show-overflow-tooltip />
        <el-table-column prop="created_at" label="创建时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.created_at) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template #default="{ row }">
            <el-button-group class="operation-buttons">
              <el-button type="primary" @click="showEditDialog(row)">
                <el-icon><Edit /></el-icon>编辑
              </el-button>
              <el-button type="danger" @click="handleDelete(row)">
                <el-icon><Delete /></el-icon>删除
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- No changes to template section -->
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const systemConfigs = ref([])
const pagination = ref({
  currentPage: 1,
  pageSize: 10,
  total: 0
})
const loading = ref({
  systemConfigs: false,
  configs: false,
  submit: false,
  refresh: false,
  batchDelete: false
})

// 搜索表单
const searchForm = reactive({
  key: '',
  value: ''
})

// 多选相关
const selectedConfigs = ref([])

// 获取系统配置列表
const fetchSystemConfigList = async () => {
  try {
    loading.systemConfigs = true
    
    // 构建查询参数
    const params = {
      page: pagination.value.currentPage,
      page_size: pagination.value.pageSize
    }
    
    if (searchForm.key) {
      params.key = searchForm.key
    }
    
    if (searchForm.value) {
      params.value = searchForm.value
    }
    
    const response = await getSystemConfigList(params)
    
    if (response && response.items) {
      systemConfigs.value = response.items
      pagination.value.total = response.total
    } else {
      systemConfigs.value = []
      pagination.value.total = 0
    }
  } catch (error) {
    console.error('获取系统配置失败:', error)
    ElMessage.error('获取系统配置列表失败')
    systemConfigs.value = []
    pagination.value.total = 0
  } finally {
    loading.systemConfigs = false
  }
}

// 多选处理
const handleSelectionChange = (selection) => {
  selectedConfigs.value = selection
}

// 批量删除
const handleBatchDelete = async () => {
  if (selectedConfigs.value.length === 0) {
    ElMessage.warning('请选择要删除的配置项')
    return
  }
  
  try {
    await ElMessageBox.confirm(
      `确定要删除选中的 ${selectedConfigs.value.length} 个配置项吗？此操作不可恢复。`,
      '批量删除确认',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    loading.batchDelete = true
    
    // 逐个删除配置项
    const promises = selectedConfigs.value.map(config => deleteSystemConfig(config.id))
    await Promise.all(promises)
    
    ElMessage.success(`成功删除 ${selectedConfigs.value.length} 个配置项`)
    selectedConfigs.value = []
    fetchSystemConfigList()
  } catch (error) {
    if (error !== 'cancel') {
      console.error('批量删除配置项失败:', error)
      ElMessage.error('批量删除配置项失败')
    }
  } finally {
    loading.batchDelete = false
  }
}

// 重置查询
const resetSearch = () => {
  searchForm.key = ''
  searchForm.value = ''
  pagination.currentPage = 1
  fetchSystemConfigList()
}
</script>

<style>
  /* No changes to style section */
</style> 